from flask import Blueprint, request, url_for, session, redirect

admin_blu = Blueprint("admin", __name__, url_prefix="/admin")

from . import views


@admin_blu.before_request
def before_request():
    # 如果不是登录页面的请求
    if not request.url.endswith(url_for("admin.admin_login")):
        # 从缓存中读取用户数据
        user_id = session.get('user_id', None)
        is_admin = session.get('is_admin', False)
        # 若用户未登录或非管理员，返回项目首页
        if not user_id or not is_admin:
            return redirect('/')
